package cn.wongshu.student.data;

import cn.wongshu.student.pojo.Student;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class StudentListData {

    private static List<Student> studentList = null;

    private static final List<Student> studentListCopy;


    static {
        if (studentList == null){
            studentList = new ArrayList<>();
        }
        Student s1 = new Student(1, "礼拜一", "111111", "为什么要上班");
        Student s2 = new Student(2, "礼拜二", "222222", "努力工作");
        Student s3 = new Student(3, "礼拜三", "333333", "继续努力工作");
        Student s4 = new Student(4, "礼拜四", "444444", "怎么好不到周末");
        Student s5 = new Student(5, "礼拜五", "555555", "明天就周末了");
        Student s6 = new Student(6, "礼拜六", "666666", "放假了舒服");
        Student s7 = new Student(7, "礼拜天1111", "777777", "为什么明天要上班");


        studentList.add(s1);
        studentList.add(s2);
        studentList.add(s3);
        studentList.add(s4);
        studentList.add(s5);
        studentList.add(s6);
        studentList.add(s7);

        studentListCopy = studentList;




    }

    //获取下一个id

    public Integer getNextId(){
        if (studentList != null){
            return studentList.size()+1;

        }else {
            return 1;
        }

    }


    //查询

    public List<Student> findAll(){
        return studentList;

    }


    //条件查询

    public List<Student> findByName(String name){

        if (name != null && !"".equals(name)){
            return studentList.stream().filter(a -> a.getName().contains(name)).collect(Collectors.toList());
        }else {
            return studentList;
        }

    }

    //删除
    public void delete(Integer[] ids){

        if (ids != null ){
            List<Integer> idList = Arrays.asList(ids);
            studentList = studentList.stream().
                    filter(a -> !idList.contains(a.getId())).collect(Collectors.toList());

        }


    }

    //修改或者添加

    public void  updateOrsave (Student student){
        if (student != null){
            if (student.getId() != null){
                studentList.set(student.getId()-1,student);
            }else {
                student.setId(getNextId());
                studentList.add(student);

            }

        }



    }



    //数据恢复

    public void newStart(){

        studentList = studentListCopy;
    }


}
